***********************************************************************************
*** Description: 	This document provides the code for replicating the 		***
***					models, and figures in the paper, "How Citizens Want 		***
***                 their Legislator to Vote," which is authored by Ruth		***
***					Dassonneville, André Blais, Semra Sevi and Jean-François 	***
***					Daoust and appears in Legislative Studies Quarterly.		***
***					It also provides code for reproducing the tables and		***
***					figures in the supplementary materials.						***
***********************************************************************************

************************************
** installing add-ons (if needed) **
************************************

	ssc install estout, replace
	
****************************	
** additional data coding **
****************************	

	use "representation.dta", clear
	
	gen scenario=. 
	replace scenario=1 if ROT_SCENARIO_YQ_1==1
	replace scenario=2 if ROT_SCENARIO_YQ_2==1
	replace scenario=3 if ROT_SCENARIO_YQ_3==1
	replace scenario=4 if ROT_SCENARIO_YQ_4==1
	replace scenario=5 if ROT_SCENARIO_YQ_5==1
	replace scenario=6 if ROT_SCENARIO_YQ_6==1
	replace scenario=7 if ROT_SCENARIO_YQ_7==1
	replace scenario=8 if ROT_SCENARIO_YQ_8==1
	tab scenario, gen(scenario_dummy)
	
	gen rot_order=. 
	replace rot_order=1 if ROT_ORDER_YQ_1==1
	replace rot_order=2 if ROT_ORDER_YQ_2==1
	replace rot_order=3 if ROT_ORDER_YQ_3==1
	replace rot_order=4 if ROT_ORDER_YQ_4==1
	replace rot_order=5 if ROT_ORDER_YQ_5==1
	replace rot_order=6 if ROT_ORDER_YQ_6==1
	tab rot_order, gen(order_dummy)

	gen pos_dep=.
	replace pos_dep=0 if scenario==2 | scenario==5 | scenario==7 | scenario==8
	replace pos_dep=1 if scenario==1 | scenario==3 | scenario==4 | scenario==6

	gen pos_maj=.
	replace pos_maj=0 if scenario==3 | scenario==5 | scenario==6 | scenario==8
	replace pos_maj=1 if scenario==1 | scenario==2 | scenario==4 | scenario==7

	gen promise=.
	replace promise=0 if scenario==4 | scenario==6 | scenario==7 | scenario==8
	replace promise=1 if scenario==1 | scenario==2 | scenario==3 | scenario==5 

	gen personal_view=YQ3
	recode personal_view 1=0 3=0 2=1 

	recode YQ1 2=0
	gen vote_for=1 if YQ1==1
	replace vote_for=0 if YQ1==0
	
	gen female=1 if resp_gender==2
	replace female=0 if resp_gender==1
	
	gen education=1 if CAEDU2==1 | CAEDU2==2 | CAEDU2==3 	// low
	replace education=2 if CAEDU2==4 | CAEDU2==5	// middle
	replace education=3 if CAEDU2==6 | CAEDU2==7 | CAEDU2==8	// high
	tab education, gen(edu_cat)
	
	gen income = USHHI3
	recode income (99=.)
	
	gen atlantic=0 if HCAL_Region1_CA!=.
	replace atlantic=1 if HCAL_Region1_CA==5 | HCAL_Region1_CA==10 | HCAL_Region1_CA==6 | HCAL_Region1_CA==4
	gen ontario=0 if  HCAL_Region1_CA!=.
	replace ontario=1 if HCAL_Region1_CA==9
	gen quebec=0 if  HCAL_Region1_CA!=.
	replace quebec=1 if HCAL_Region1_CA==11
	gen prairie=0 if HCAL_Region1_CA!=.
	replace prairie=1 if HCAL_Region1_CA==3 | HCAL_Region1_CA==12 | HCAL_Region1_CA==1
	gen bc=0 if HCAL_Region1_CA!=.
	replace bc=1 if HCAL_Region1_CA==2
	
	clonevar votechoice=YQ4 
	recode votechoice (4=8) (5=8) (6=8)		// small parties in 'other' category
	
	label variable pos_dep "MP personally in favor"
	label variable pos_maj "Majority citizens in favor"
	label variable promise "MP promised voting in favor"
	label variable personal_view "R. wants fewer immigrants"
	label variable vote_for "MP should vote in favor"
	label variable female "Female"
	label variable resp_age "Age" 
	label variable edu_cat1 "Low educated"
	label variable edu_cat2 "Middle educated"
	label variable edu_cat3 "High educated"
	label variable income "Income"
	label variable atlantic "Atlantic provinces"
	label variable ontario "Ontario"
	label variable quebec "Quebec"
	label variable prairie "Prairie provinces"
	label variable bc "British Columbia"
	label variable votechoice "Federal vote intention"
	
********************************	
** main analyses in the paper **	
********************************	
	
	*Table 1
	
	eststo m1: reg vote_for i.pos_dep i.pos_maj i.promise [pweight=Weightvar]
	
	* Figure 1
		margins, dydx(pos_dep pos_maj promise)
		marginsplot, plotopts(msymbol(O) msize(large)) ytitle("Marginal effects") xtitle("")   ///
		recast(scatter) recastci(rspike) title("") xscale(range(0.5 3.5)) ylabel(0(0.05)0.3) ///
		xlabel(1 "MP is in favor" 2 "Majority is in favor" 3 "MP promised to vote in favor", labsize(small)) ///
		plotregion(ilcolor(black) margin(zero))
		graph export fig1_rr.pdf, replace
	
	eststo m2: reg vote_for pos_dep pos_maj promise i.rot_order [pweight=Weightvar]

	eststo m3: reg vote_for i.pos_dep##i.pos_maj i.pos_dep##i.promise i.promise##i.pos_maj ///
	i.promise##i.pos_maj##i.pos_dep i.rot_order [pweight=Weightvar]

	eststo m4: reg vote_for i.personal_view i.pos_dep i.pos_maj i.promise  i.rot_order [pweight=Weightvar]
		margins, dydx(personal_view) 

	eststo m5: reg vote_for i.personal_view##i.pos_dep ///
	i.personal_view##i.pos_maj i.personal_view##i.promise i.rot_order [pweight=Weightvar]

	esttab m1 m2 m3 m4 m5 using table1_rr.rtf, b(3) se(3) pr2 nogap replace
	
	*Figure 2
		margins, dydx(pos_dep) at(personal_view=(0 1))
		marginsplot, plotopts(msymbol(O) msize(large)) ytitle("MP position") title("")   ///
		recast(scatter) recastci(rspike) xtitle("") xscale(range(-0.5 1.5)) ylabel(0(0.1)0.35) ///
		xlabel(0 `" "Wants more/same" "immigrant" "' 1 `" "Wants fewer" "immigrant" "') plotregion(ilcolor(black) margin(zero)) saving(pview_mp.gph, replace) nodraw
	
		margins, dydx(pos_maj) at(personal_view=(0 1))
		marginsplot ,  plotopts(msymbol(O) msize(large)) ytitle("Majority position") title("")   ///
		recast(scatter) recastci(rspike) xtitle("") xscale(range(-0.5 1.5)) ylabel(0(0.1)0.35) ///
		xlabel(0 `" "Wants more/same" "immigrant" "' 1 `" "Wants fewer" "immigrant" "' ) plotregion(ilcolor(black) margin(zero)) saving(pview_majority.gph, replace) nodraw
	
		margins, dydx(promise) at(personal_view=(0 1))
		marginsplot, plotopts(msymbol(O) msize(large)) ytitle("Promise") title("")   ///
		recast(scatter) recastci(rspike) xtitle("") xscale(range(-0.5 1.5)) ylabel(0(0.1)0.35) ///
		xlabel(0 `" "Wants more/same" "immigrant" "' 1 `" "Wants fewer" "immigrant" "' ) plotregion(ilcolor(black) margin(zero)) saving(pview_promise.gph, replace) nodraw
	
		graph combine pview_mp.gph pview_majority.gph pview_promise.gph, xsize(2) ysize(1) ///
		col(3) 
		graph export pview_combined.pdf, replace 

*****************************
** supplementary materials **
*****************************	
	
	** Appendix 1. balance test
	
	ttest female, by(pos_dep)
	ttest female, by(pos_maj)
	ttest female, by(promise)
	
	ttest resp_age, by(pos_dep)
	ttest resp_age, by(pos_maj)
	ttest resp_age, by(promise)
	
	ttest edu_cat1, by(pos_dep)
	ttest edu_cat1, by(pos_maj)
	ttest edu_cat1, by(promise)
	
	ttest edu_cat2, by(pos_dep)
	ttest edu_cat2, by(pos_maj)
	ttest edu_cat2, by(promise)
	
	ttest edu_cat3, by(pos_dep)
	ttest edu_cat3, by(pos_maj)
	ttest edu_cat3, by(promise)
	
	ttest income, by(pos_dep)
	ttest income, by(pos_maj)
	ttest income, by(promise)
	
	ttest atlantic, by(pos_dep)
	ttest atlantic, by(pos_maj)
	ttest atlantic, by(promise)
	
	ttest ontario, by(pos_dep)
	ttest ontario, by(pos_maj)
	ttest ontario, by(promise)
	
	ttest quebec, by(pos_dep)
	ttest quebec, by(pos_maj)
	ttest quebec, by(promise)
	
	ttest prairie, by(pos_dep)
	ttest prairie, by(pos_maj)
	ttest prairie, by(promise)
	
	ttest bc, by(pos_dep)
	ttest bc, by(pos_maj)
	ttest bc, by(promise)
	
	** Appendix 2. explaining citizens' views on immigration
	
	eststo m1: reg personal_view female resp_age i.education atlantic ontario quebec prairie [pweight=Weightvar]
	
	esttab m1 using App2table1.rtf, b(3) se(3) wide r2 nogap replace

	** Appendix 2. Figure 1. 
	margins , dydx(female resp_age i.education atlantic ontario quebec prairie )
	marginsplot, horizontal recast(scatter) xline(0) ytitle("") ///
	ylab(1 "Female (ref:male)" 2 "Age" 3 "Middle educated (ref: low)" ///
	4 "High educated (ref: low)" 5 "Atlantic (ref: BC)" 6 "Ontario (ref: BC)" ///
	7 "Quebec (ref: BC)" 8 "Prairies (ref: BC)") ysc(r(.5 8.5)) ///
	xtitle("Marginal effects on probability of wanting fewer immigrants") xsc(r(-.33 .21)) ///
	title("") plotregion(ilcolor(black) margin(zero)) plotopt(msize(medium) msymbol(circle)) saving(App2fig1.gph, replace)
				
	** Appendix 3. interactions with education, gender, age 
	
	eststo m1: reg vote_for personal_view i.education##i.pos_dep i.education##i.pos_maj i.education##i.promise  i.rot_order [pweight=Weightvar]
	eststo m2: reg vote_for personal_view i.female##i.pos_dep i.female##i.pos_maj i.female##i.promise  i.rot_order [pweight=Weightvar]
	eststo m3: reg vote_for personal_view c.resp_age##i.pos_dep c.resp_age##i.pos_maj c.resp_age##i.promise  i.rot_order [pweight=Weightvar]
	eststo m4: reg vote_for personal_view b7.votechoice##i.pos_dep b7.votechoice##i.pos_maj b7.votechoice##i.promise  i.rot_order [pweight=Weightvar]
	
	esttab m1 m2 m3 m4 using App3table1.rtf, b(3) se(3)  r2 nogap replace 

	** Appendix 4. post-treatment bias
	
	ttest personal_view, by(pos_dep)
	ttest personal_view, by(pos_maj)
	ttest personal_view, by(promise)
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
